﻿Imports Arwani
Imports System.Data
Imports System.Web
Imports System.Web.UI
Imports System.Collections.Generic
Imports System.Web.UI.WebControls
Imports System.IO
Imports System.Drawing


Partial Class Backend_RaffleList
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Session("ADMIN_USER") IsNot Nothing) Then
            BindData()
        Else
            Response.Redirect("Default.aspx")

        End If

    End Sub

    Public Sub BindData()
        Dim awrcon As New ArwaniController
        Dim str As String = "Select * from Raffle"
        Dim dtTable As DataTable = awrcon.ExecuteQuery(str)
        If dtTable.Rows.Count > 0 Then
            gvRaffle.DataSource = dtTable
            gvRaffle.DataBind()
        End If
    End Sub

    Protected Sub gvRaffle_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvRaffle.PageIndexChanging
        gvRaffle.PageIndex = e.NewPageIndex

        BindData()

    End Sub

    Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim item As String = e.Row.Cells(0).Text
            Dim username As String = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ComapanyName"))
            Dim lnkbtnresult As LinkButton = DirectCast(e.Row.FindControl("lnkdelete"), LinkButton)
            'raising javascript confirmationbox whenver user clicks on link button
            lnkbtnresult.Attributes.Add("onclick", "javascript:return ConfirmationBox('" & username & "')")
        End If
    End Sub

    Protected Sub lnkdelete_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim lnkbtn As LinkButton = TryCast(sender, LinkButton)
        'getting particular row linkbutton
        Dim gvrow As GridViewRow = TryCast(lnkbtn.NamingContainer, GridViewRow)
        'getting userid of particular row
        Dim userid As Integer = Convert.ToInt32(gvRaffle.DataKeys(gvrow.RowIndex).Value.ToString())
        Dim username As String = gvrow.Cells(0).Text
        Dim awrcon As New ArwaniController
        Dim str As String = "delete from Raffle where ID=" & userid
        awrcon.ExecuteNonQuery(str)
        BindData()
        'Displaying alert message after successfully deletion of user
        'ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "alertmessage", "javascript:alert('" & username & " details deleted successfully')", True)
        'Dim script As String = "alert(""Please select atleast one Media!"");"
        'ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "ServerControlScript", script, True)
    End Sub
    Protected Sub btnExcel_Click(ByVal sender As Object, ByVal e As EventArgs)


        Dim awrcon As New ArwaniController
        Dim str As String = "Select * from Raffle"
        Dim dtTable As DataTable = awrcon.ExecuteQuery(str)



        'Create a dummy GridView

        Dim GridView1 As New GridView()

        GridView1.AllowPaging = False

        GridView1.DataSource = dtTable

        GridView1.DataBind()



        Response.Clear()

        Response.Buffer = True

        Response.AddHeader("content-disposition", "attachment;filename=Raffle.xls")

        Response.Charset = ""

        Response.ContentType = "application/vnd.ms-word "

        Dim sw As New StringWriter()

        Dim hw As New HtmlTextWriter(sw)

        GridView1.RenderControl(hw)

        Response.Output.Write(sw.ToString())

        Response.Flush()

        Response.End()
    End Sub
End Class
